import 'dart:convert';

import 'package:dulu/config/global.dart';
import 'package:dulu/iconfont.dart';
import 'package:dulu/manager/main_manager.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class MoodEditorPage extends StatefulWidget {
  MoodEditorPage({Key key, this.year, this.month, this.day}) : super(key: key);
  final int year;
  final int month;
  final int day;
  @override
  _MoodEditorPageState createState() => _MoodEditorPageState();
}

class _MoodEditorPageState extends State<MoodEditorPage> {
  TextEditingController _textController = TextEditingController();
  String content = '';
  @override
  void initState() {
    _textController.text = '';
    super.initState();
  }

  String getWeekString() {
    DateTime t = DateTime.parse(
        '${widget.year}-${widget.month > 9 ? widget.month : "0" + widget.month.toString()}-${widget.day > 9 ? widget.day : "0" + widget.day.toString()}');
    List<String> weeksString = ['一', '二', '三', '四', '五', '六', '日'];
    return weeksString[t.weekday - 1];
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return Scaffold(
      body: Container(
        child: Column(
          children: [
            Container(
              padding: EdgeInsets.fromLTRB(15, Global.padding.top, 13, 20),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  InkWell(
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                    child: Container(
                      width: 40,
                      child: Icon(IconFont.close),
                    ),
                  ),
                  InkWell(
                    onTap: () async {
                      var queryRes = await _mainManager.queryDayMood(
                          widget.year, widget.month, widget.day);
                      if (queryRes.length > 0) {
                        int timestamp = DateTime.now().millisecondsSinceEpoch;
                        List momt = json.decode(queryRes[0]['momt']);
                        momt.add({'timestamp': timestamp, 'content': content});
                        _mainManager.updateMoodMomt(queryRes[0]['id'], momt);
                      }
                      Navigator.of(context).pop();
                    },
                    child: Container(
                      width: 40,
                      child: Icon(IconFont.bianzu3),
                    ),
                  ),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.fromLTRB(20, 10, 20, 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(
                        widget.day.toString(),
                        style: TextStyle(
                          fontSize: 50,
                          fontWeight: FontWeight.w600,
                          color: Global.fontColor,
                          fontFamily: 'Kuaile',
                        ),
                      ),
                      SizedBox(width: 10),
                      Column(
                        mainAxisAlignment: MainAxisAlignment.start,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            '星期${getWeekString()}',
                            style: TextStyle(
                              fontSize: 20,
                              fontWeight: FontWeight.w600,
                              color: Global.fontColor,
                              fontFamily: 'Kuaile',
                            ),
                          ),
                          SizedBox(height: 3),
                          Text(
                            '${widget.year}年${widget.month}月',
                            style: TextStyle(
                              fontSize: 15,
                              fontWeight: FontWeight.w500,
                              color: Global.fontColor,
                              fontFamily: 'Kuaile',
                            ),
                          ),
                        ],
                      )
                    ],
                  ),
                  // Text(
                  //   '12:31',
                  //   style: TextStyle(
                  //     fontSize: 20,
                  //     fontWeight: FontWeight.w500,
                  //     color: Global.fontColor,
                  //     fontFamily: 'Kuaile',
                  //   ),
                  // ),
                ],
              ),
            ),
            ConstrainedBox(
              constraints: BoxConstraints(
                maxWidth: MediaQuery.of(context).size.width - 50,
              ),
              child: TextField(
                controller: _textController,
                maxLines: 15,
                style: TextStyle(
                  color: Global.fontColor,
                  fontFamily: 'Kuaile',
                  fontSize: 20,
                  height: 1.7,
                  letterSpacing: 2,
                ),
                decoration: InputDecoration.collapsed(
                  hintText: '记录你现在的心情瞬间吧！',
                  hintStyle: TextStyle(
                    fontFamily: 'Kuaile',
                    color: Global.fontSecondColor.withOpacity(0.5),
                    fontSize: 20,
                  ),
                ),
                onSubmitted: (text) {},
                onChanged: (text) {
                  setState(() {
                    content = text;
                  });
                },
              ),
            )
          ],
        ),
      ),
    );
  }
}
